package com.hc.kotlin

import kotlin.math.roundToInt

fun main5() {
    //-----字符串操作-----
    var a = "Hello World"
    println(a.substring(0 until 2))
    var b = "Hello,World,Jack"
    val(v1,v2,v3) = b.split(",") //解构
    println("$v1 $v2 $v3")
    var c = "Hello World"
    println(c.replace(Regex("[aeiou]")){
        when(it.value){
            "a"->"1"
            "e"->"2"
            "i"->"3"
            "o"->"4"
            "u"->"5"
            else->it.value
        }
    })
    println(a==c)
    println(a===c)
    var d = "hello World".capitalize()
    println(a==d)
    println(a===d)

    d.forEach {
        println(it)
    }
}
fun main6() {
    //-----数据类型转换-----
//    var a = "8.88".toInt()
//    println(a)
    var b = "8.88".toIntOrNull()?:3
    println(b)
    var c = "%.2f".format(8.16579)
    println(c)
    var d = "%.3s".format("8.16579")
    println(d)
    var e = 8.6578.toInt() //直接舍弃小数
    println(e)
    var f = 8.6251.roundToInt() //四舍五入
    println(f)
}
fun main() {
    //-----标准库函数-----
    var a = "Hello"
    val aapply = a.apply {//自身执行什么 返回自身
        a.substring(1 until 3)
    }
    println(aapply)

    var b = "Hello"
    val blet = b.let {//执行什么传入自身 返回执行结果
        val s = it.substring(2 until 3)
        s
    }
    println(blet)

    var c = 2
    val crun = c.run {//可选自身执行什么 返回执行结果
        4+4
    }

    println(crun)

    var d = 4
    val dwith = with(d) {//run的变体 需要传一个值参 返回执行结果
        this + this
    }
    println(dwith)

    var e =5
    val ealso = e.also {//执行什么传入自身 返回自身
        it + 5
    }.also {
        it + 5
    }
    println(ealso)

    var f = 5
    val ftakeif = f.takeIf {//true返回自身,false返回null
        it > 5
    }
    println(ftakeif)

    var g = 5
    val gtakeUnless = g.takeUnless {//与takeif相反
        it > 5
    }
    println(gtakeUnless)
}